//
//  ViewController.m
//  SiriSoupChefOCDemo
//
//  Created by Allison on 2023/10/24.
//

#import "ViewController.h"
#import <Intents/Intents.h>
#import <IntentsUI/IntentsUI.h>
#import "SiriIntents.h"

@interface ViewController () <INUIAddVoiceShortcutButtonDelegate,INUIAddVoiceShortcutViewControllerDelegate,INUIEditVoiceShortcutViewControllerDelegate>

@property (nonatomic, strong) SiriIntents API_AVAILABLE(ios(12.0)) *intent;
@property (nonatomic, strong) INUIAddVoiceShortcutButton API_AVAILABLE(ios(12.0)) *shortcutButton;


@end

@implementation ViewController

- (SiriIntents *)intent API_AVAILABLE(ios(12.0)){
    if (!_intent) {
        _intent = [[SiriIntents alloc] init];
        _intent.suggestedInvocationPhrase =  @"开始执行";;   //在Siri语音设置时显示的建议设置唤起文字
    }
    return _intent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    if (@available(iOS 12.0, *)) {
        _shortcutButton = [[INUIAddVoiceShortcutButton alloc] initWithStyle:INUIAddVoiceShortcutButtonStyleWhiteOutline];
        _shortcutButton.shortcut = [[INShortcut alloc] initWithIntent:self.intent];
        _shortcutButton.translatesAutoresizingMaskIntoConstraints = false;
        _shortcutButton.delegate = self;
        [self.view addSubview:self.shortcutButton];

        
        // 1.为shortcutButton添加 width 约束和 height 约束
       NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self.shortcutButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:200];
        NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.shortcutButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:50];
        [self.shortcutButton addConstraint:widthConstraint];
        [self.shortcutButton addConstraint:heightConstraint];
        
      // 2.为shortcutButton添加top 约束和 center 约束
        NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:self.shortcutButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:100];
        NSLayoutConstraint *centerXConstraint = [NSLayoutConstraint constraintWithItem:self.shortcutButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
        [self.view addConstraint:topConstraint];
        [self.view addConstraint:centerXConstraint];

    }
}

#pragma mark - INUIAddVoiceShortcutButtonDelegate
- (void)presentAddVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)addVoiceShortcutViewController forAddVoiceShortcutButton:(INUIAddVoiceShortcutButton *)addVoiceShortcutButton API_AVAILABLE(ios(12.0)){
    addVoiceShortcutViewController.delegate = self;
    [self presentViewController:addVoiceShortcutViewController animated:YES completion:nil];
}
- (void)presentEditVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)editVoiceShortcutViewController forAddVoiceShortcutButton:(INUIAddVoiceShortcutButton *)addVoiceShortcutButton API_AVAILABLE(ios(12.0)){
    editVoiceShortcutViewController.delegate = self;
    [self presentViewController:editVoiceShortcutViewController animated:YES completion:nil];
}
#pragma mark - INUIAddVoiceShortcutViewControllerDelegate
- (void)addVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)controller didFinishWithVoiceShortcut:(INVoiceShortcut *)voiceShortcut error:(NSError *)error
API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YES completion:nil];
}
- (void)addVoiceShortcutViewControllerDidCancel:(INUIAddVoiceShortcutViewController *)controller API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - INUIEditVoiceShortcutViewControllerDelegate
- (void)editVoiceShortcutViewControllerDidCancel:(INUIEditVoiceShortcutViewController *)controller API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YES completion:nil];
}
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didUpdateVoiceShortcut:(INVoiceShortcut *)voiceShortcut error:(NSError *)error API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YES completion:nil];
}
- (void)editVoiceShortcutViewController:(INUIEditVoiceShortcutViewController *)controller didDeleteVoiceShortcutWithIdentifier:(NSUUID *)deletedVoiceShortcutIdentifier API_AVAILABLE(ios(12.0)){
    [controller dismissViewControllerAnimated:YES completion:nil];
}



////懒加载 这里的title 和 sceneID 就是上面创建的两个字段，用于告诉siri这个绑定的指令是什么，方便当Siri触发后，再将这个字段返回给我们，让我们做业务处理
//- (SiriIntents *)intentAPI_AVAILABLE(ios(12.0)){
//    if (!_intent) {
//        _intent = [[SiriIntents] init];
//        _intent.title = @"自定义Title";
//        _intent.sceneID = @"123456”;
////在Siri语音设置时显示的建议设置唤起文字
//        _intent.suggestedInvocationPhrase = @"开始执行";
//    }
//    return_intent;
//}

@end
